package masterjava.soa.smsgate.smstraffic;

import masterjava.common.exception.CommonExceptionType;
import masterjava.common.exception.FaultFactory;
import masterjava.common.http.HttpUtil;
import masterjava.common.ws.WebServiceFault;
import masterjava.common.xml.JaxbSingleParser;
import masterjava.soa.smsgate.ISmsProvider;
import masterjava.soa.smsgate.SmsGateExceptionType;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Reader;

import static masterjava.soa.smsgate.SmsGateSetting.get;

@Local(ISmsProvider.class)
@Stateless(name = "SmsProvider", mappedName = "ejb/SmsTrafficGate")
@WebService
public class SmsTrafficGate implements ISmsProvider {
    private static final HttpClient HTTP_CLIENT = new DefaultHttpClient();

    private static JaxbSingleParser<SendSmsResponse> JAXB = new JaxbSingleParser<SendSmsResponse>(SendSmsResponse.class);

    @Override
    public void sendSms(String originator, String phone, String message) throws WebServiceFault {
        HttpPost post = null;
        Reader reader = null;
        try {
            post = new HttpPost(get().URL.get().toURI());
            HttpParams params = HttpUtil.createHttpParams(get().LOGIN, get().PASSWORD);
            params.setParameter("originator", originator);
            params.setParameter("phones", phone);
            params.setParameter("message", message);
            params.setParameter("rus", "1");
            post.setParams(params);
            HttpResponse response = HTTP_CLIENT.execute(post);
            StatusLine status = response.getStatusLine();
            if (status.getStatusCode() != HttpStatus.SC_OK) {
                throw FaultFactory.createFault(SmsGateExceptionType.CONNECTION_ERROR, status.getStatusCode(), status.getReasonPhrase());
            }
            reader = HttpUtil.getReader(response.getEntity());
            SendSmsResponse result = JAXB.unmarshal(reader);
            if (result.code != 0) {
                throw FaultFactory.createFault(SmsGateExceptionType.SMS_GATE_ERROR, result.code, result.description);
            }
        } catch (Exception e) {
            throw FaultFactory.createFault(CommonExceptionType.INTERNAL_ERROR, e);
        } finally {
            IOUtils.closeQuietly(reader);
        }
    }

    @XmlRootElement(name = "reply")
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class SendSmsResponse {
        @XmlElement(required = true)
        String result;
        @XmlElement(required = true)
        int code;
        @XmlElement(required = true)
        String description;
    }
}
